package it.traveldream.entities;

import it.traveldream.dto.ProductDTO;

import java.io.Serializable;

import javax.persistence.*;

/**
 * Entity Product
 * It represents a basic piece of a Travel Package.
 * Class Product is the superclass for any kind of Product
 *
 */
@Entity
@Table(name = "Product")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPEID", discriminatorType=DiscriminatorType.INTEGER)

@NamedQuery(
		name = "getAllProductInLocation",
		query = "SELECT p,p.type FROM Product p WHERE p.location = :location"
		)
public abstract class Product implements Serializable {

	@Id
	@GeneratedValue
	protected Long productId;
	@Column(nullable = false)
	protected String name;
	protected static final long serialVersionUID = 1L;

	public Product() {
		super();
	}   
	
	@ManyToOne(optional = false)
	@JoinColumn(name = "LOCATIONID", referencedColumnName = "LOCATIONID", nullable = false)
	protected Location location;
	
	@ManyToOne(optional = false)
    @JoinColumn(name = "TYPEID", referencedColumnName = "PRODUCTTYPEID")
	private ProductType type;

	public Location getLocation() {
		return this.location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public Long getProductId() {
		return this.productId;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
   
	public ProductDTO toDTO(){
		ProductDTO dto = new ProductDTO();
		dto.setProductId(this.productId);
		dto.setName(this.name);
		dto.setLocation(this.location.toDTO());
		
		return dto;
	}
}
